package tw.cating.karmasaver.alarm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import tw.cating.karmasaver.R;
import tw.cating.karmasaver.intef.selectedChangeListener;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class alarmAdapter extends BaseAdapter{	
	protected static final String ALARM_COUNT = "alarmCount";
	protected static final String ALARM_HOUR_PREFIX = "alarm_Hour_";
	protected static final String ALARM_MINUTE_PREFIX = "alarm_Minute_";
	
	Context mContext = null;
	ArrayList<alarmMeta> mList = new ArrayList<alarmMeta>();
	selectedChangeListener l = null;
	
	public alarmAdapter(Context context) {
		mContext = context;
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
		mList.clear();
		int count = sp.getInt(ALARM_COUNT, 0);
		for (int i = 0 ; i < count ; i++) {
			String KeyHour = ALARM_HOUR_PREFIX + i;
			String KeyMinute = ALARM_MINUTE_PREFIX + i;
			alarmMeta meta = new alarmMeta(
					sp.getInt(KeyHour, 0),
					sp.getInt(KeyMinute, 0)
			);
			
			if (meta != null) {
				mList.add(meta);
			}
		}
		
		sort();
	}
	
	public void writebackSharedPreferences() {
		SharedPreferences.Editor sp = PreferenceManager.getDefaultSharedPreferences(mContext).edit();
		int count = mList.size();
		sp.putInt(ALARM_COUNT, count);
		for (int i = 0 ; i < count ; i++) {
			String KeyHour = ALARM_HOUR_PREFIX + i;
			String KeyMinute = ALARM_MINUTE_PREFIX + i;
			alarmMeta meta = mList.get(i);
			sp.putInt(KeyHour, meta.mHour);
			sp.putInt(KeyMinute, meta.mMin);
		}
		sp.commit();
	}
	
	public void sort() {
		Comparator<alarmMeta> comperator = new Comparator<alarmMeta>() {
			public int compare(alarmMeta m1, alarmMeta m2) {
				if (m1.mHour > m2.mHour) {
					return 1;
				} else if (m1.mHour < m2.mHour) {
					return -1;
				} else { // m1.mHour == m2.mHour
					if (m1.mMin > m2.mMin)
						return 1;
					else if (m1.mMin < m2.mMin)
						return -1;
					else
						return 0;
				}
			}
		};
		Collections.sort(mList, comperator);
	}
	
	public void addAlarm(alarmMeta alarm) {
		mList.add(alarm);
		
		sort();
		
		this.notifyDataSetChanged();
	}
	
	public void deleteCheckedItem() {
		for (int i = mList.size(); i > 0; i--) {
			alarmMeta alarm = mList.get(i - 1);
			if (alarm.bIsChecked)
				mList.remove(alarm);
		}
		
		if (l != null)
			l.onChanged();
		
		this.notifyDataSetChanged();
	}
	
	public void setSelectedChangeListener(selectedChangeListener listener) {
		l = listener;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mList == null ? 0 : mList.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList == null ? null : mList.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = ((Activity)mContext).getLayoutInflater().inflate(R.layout.listitem, null);
			CheckBox cb = (CheckBox)convertView.findViewById(R.id.checked);
			cb.setVisibility(View.VISIBLE);
		}
		
		final alarmMeta alarm = (alarmMeta)getItem(position);
		CheckBox cb = (CheckBox)convertView.findViewById(R.id.checked);
		cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				alarm.bIsChecked = isChecked;
				if (l != null) {
					l.onChanged();
				}
			}
		});
		TextView tv = (TextView)convertView.findViewById(R.id.rssdesc);
		tv.setText(String.format("%02d:%02d", alarm.mHour, alarm.mMin));
		return convertView;
	}
}
